package com.wj.hourupdate;

import java.util.List;

import com.j1tth4.mobile.core.util.ImageLoader;
import com.wj.hourupdate.datasource.NewsData;
import com.wj.hourupdate.util.Logger;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NewsListAdapter extends BaseAdapter{
	private Context c;
	private LayoutInflater inflater;
	private List<NewsData.News> newsDetailLst;
	private ImageLoader imageLoader;
	private ImageLoader imageLoaderLarge;
	private Logger log;
	private final String logFolder = "HourUpdateLog";
	private final String imgFolder = "HournewsPicture";
	
	public NewsListAdapter(Context c, List<NewsData.News> newsDetailLst){
		this.c = c;
		this.newsDetailLst = newsDetailLst;
		inflater = LayoutInflater.from(c);
		log = new Logger(c, logFolder, this.getClass().getName());

		imageLoader = new ImageLoader(c, 0, imgFolder);
		imageLoaderLarge = new ImageLoader(c, 0, imgFolder, ImageLoader.IMAGE_SIZE.LARGE);
	}
	
	@Override
	public int getCount() {
		return newsDetailLst == null ? 0 : newsDetailLst.size();
	}

	@Override
	public NewsData.News getItem(int position) {
		return newsDetailLst.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final NewsData.News newsDetail = newsDetailLst.get(position);
		ViewHolder holder;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.news_title_template, null);
			holder = new ViewHolder();
			holder.imgNewsLogo = (ImageView) convertView.findViewById(R.id.imageView1);
			holder.title = (TextView) convertView.findViewById(R.id.textView1);
			holder.publishDate = (TextView) convertView.findViewById(R.id.textView2);
			holder.detail = (TextView) convertView.findViewById(R.id.textView3);
			holder.newsUrl = (TextView) convertView.findViewById(R.id.textView4);
			holder.imgNews = (ImageView) convertView.findViewById(R.id.imageView2);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}

		imageLoader.displayImage(newsDetail.getCnlogo(), holder.imgNewsLogo);
		imageLoaderLarge.displayImage(newsDetail.getCdnimage(), holder.imgNews);
		holder.title.setText(newsDetail.getCdntitle());
		holder.publishDate.setText(newsDetail.getCdnpubdate());
		holder.detail.setText(newsDetail.getCdndesc());
		holder.newsUrl.setText(newsDetail.getCdnlink());
		holder.newsUrl.setPaintFlags(holder.newsUrl.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
		
		holder.newsUrl.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(c, NewsDetailActivity.class);
				intent.putExtra("newsTitle", newsDetail.getCdntitle());
				intent.putExtra("newsUrl", newsDetail.getCdnlink());
				c.startActivity(intent);
			}
			
		});
		
//		holder.imgNews.setOnClickListener(new OnClickListener(){
//
//			@Override
//			public void onClick(View v) {
//				v = inflater.inflate(R.layout.news_large_image_layout, null);
//				ImageView largeImg = (ImageView) v.findViewById(R.id.imageView1);
//				
//				imageLoader = new ImageLoader(c, 0, imgFolder, ImageLoader.IMAGE_SIZE.LARGE);
//				imageLoader.displayImage(newsDetail.getCdnimage(), largeImg);
//				Dialog d = new Dialog(c, R.style.CustomDialog);
//				d.setContentView(v);
//				d.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, 
//						WindowManager.LayoutParams.WRAP_CONTENT);
//				d.show();
//			}
//		});
		
		convertView.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
			
		});
		return convertView;
	}
	
	private class ViewHolder{
		private TextView title;
		private TextView publishDate;
		private TextView detail;
		private TextView newsUrl;
		private ImageView imgNews;
		private ImageView imgNewsLogo;
	}
}
